import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

// 商城模块
const home = r => require.ensure([], () => r(require('../pages/home/index')), 'home')// 首页
const error = r => require.ensure([], () => r(require('../pages/error/404')), '404')// 首页
const login = r => require.ensure([], () => r(require('../pages/login/index')), 'login')// 用户登录
const regist = r => require.ensure([], () => r(require('../pages/regist/index')), 'regist')// 用户注册
const password = r => require.ensure([], () => r(require('../pages/password/index')), 'password')// 找回密码
const cart = r => require.ensure([], () => r(require('../pages/cart/index')), 'cart')// 购物车
const comment = r => require.ensure([], () => r(require('../pages/comment/index')), 'comment')// 评论
const gocomment = r => require.ensure([], () => r(require('../pages/comment/comment')), 'gocomment')// 评论功能
const commentdetail = r => require.ensure([], () => r(require('../pages/comment/commentdetail')), 'commentdetail')// 评论详情
const commentReply = r => require.ensure([], () => r(require('../pages/comment/commentReply')), 'commentReply')// 评论回复
const cartConfirm = r => require.ensure([], () => r(require('../pages/cart/confirm')), 'cartConfirm')// 购买确认
const cartSuccess = r => require.ensure([], () => r(require('../pages/cart/success')), 'cartSuccess')// 购买成功
const item = r => require.ensure([], () => r(require('../pages/item/index')), 'item')// 商品列表
const itemDetail = r => require.ensure([], () => r(require('../pages/item/detail')), 'orderDetail')// 商品详情
const goodsDetail = r => require.ensure([], () => r(require('../pages/item/goodsDetail')), 'goodsDetail')// 商品详情
const classify = r => require.ensure([], () => r(require('../pages/item/classify')), 'itemClassify')// 商品分类
const search = r => require.ensure([], () => r(require('../pages/item/search')), 'search')// 商品搜索
// 用户中心模块
const user = r => require.ensure([], () => r(require('../pages/user/index')), 'user')// 用户中心
const userInfo = r => require.ensure([], () => r(require('../pages/user/userInfo')), 'userInfo')// 用户修改
const userPasswordEdit = r => require.ensure([], () => r(require('../pages/user/passwordEdit')), 'userPasswordEdit')// 用户密码修改
const userWallet = r => require.ensure([], () => r(require('../pages/user/wallet')), 'userWallet')// 用户钱包
const rechargeDetail = r => require.ensure([], () => r(require('../pages/user/rechargeDetail')), 'rechargeDetail')// 用户钱包详情
const userIntegral = r => require.ensure([], () => r(require('../pages/user/integral')), 'userIntegral')// 用户积分
const userOrder = r => require.ensure([], () => r(require('../pages/user/order')), 'userOrder')// 用户订单
const userOrderDetail = r => require.ensure([], () => r(require('../pages/user/orderDetail')), 'userOrderDetail')// 订单详情
const userAddress = r => require.ensure([], () => r(require('../pages/user/address')), 'userAddress')// 地址列表
const userAddressDetail = r => require.ensure([], () => r(require('../pages/user/addressDetail')), 'userAddressDetail')// 地址详情
const userAddressAdd = r => require.ensure([], () => r(require('../pages/user/addressAdd')), 'userAddressAdd')// 地址添加
const userAddressEdit = r => require.ensure([], () => r(require('../pages/user/addressEdit')), 'userAddressEdit')// 地址修改
const userMessage = r => require.ensure([], () => r(require('../pages/user/message')), 'userMessage')// 用户消息
const userMessageDetail = r => require.ensure([], () => r(require('../pages/user/messageDetail')), 'userMessageDetail')// 用户消息详情
const searchItemList = r => require.ensure([], () => r(require('../pages/item/searchItemList')), 'searchItemList')// 查询的商品列表
const itemList = r => require.ensure([], () => r(require('../pages/item/itemList')), 'itemList')// 商品分类列表
const uploadDemo = r => require.ensure([], () => r(require('../pages/demo/upload')), 'uploadDemo')// 上传例子
VueRouter.prototype.goBack = function () {
  this.isBack = true
  // window.history.go(-1)
}
window.addEventListener('popstate', function (e) {
  this.isBack = true
}, false)
// requireAuth: true表示进入这个路由是需要登录的
// 使用vue路由
export default new VueRouter({
  routes: [
    {
      path: '/',
      redirect: '/home'
    }, {
      path: '/home',
      name: 'home',
      component: home,
      meta: {
        keepAlive: true
      }
    }, {
      path: '/404',
      name: '错误404',
      component: error,
      meta: {
        keepAlive: true
      }
    }, {
      path: '/demo/upload',
      name: '上传',
      component: uploadDemo
    }, {
      path: '/item',
      name: 'item',
      component: item,
      meta: {
        keepAlive: true
      }
    }, {
      path: '/item/detail',
      name: 'itemDetail',
      component: itemDetail,
      meta: {
        keepAlive: false
      }
    }, {
      path: '/item/goodsDetail',
      name: 'goodsDetail',
      component: goodsDetail,
      meta: {
        keepAlive: false
      }
    }, {
      path: '/classify',
      name: 'classify',
      component: classify,
      meta: {
        keepAlive: true
      }
    }, {
      path: '/search',
      name: 'search',
      component: search,
      meta: {
        keepAlive: true
      }
    }, {
      path: '/login',
      name: 'login',
      component: login,
      meta: {
        keepAlive: true
      }
    }, {
      path: '/regist',
      name: 'regist',
      component: regist,
      meta: {
        keepAlive: true
      }
    }, {
      path: '/password',
      name: 'password',
      component: password
    }, {
      path: '/cart',
      name: 'cart',
      meta: {
        requireAuth: true
      },
      component: cart
    }, {
      path: '/comment',
      name: 'comment',
      component: comment
    }, {
      path: '/comment/comment',
      name: 'gocomment',
      component: gocomment
    }, {
      path: '/comment/commentdetail',
      name: 'commentdetail',
      component: commentdetail
    }, {
      path: '/comment/commentReply',
      name: 'commentReply',
      component: commentReply,
      meta: {
        requireAuth: true
      }
    }, {
      path: '/cart/confirm',
      name: 'cartConfirm',
      meta: {
        requireAuth: true
      },
      component: cartConfirm
    }, {
      path: '/cart/success',
      meta: {
        requireAuth: true
      },
      name: 'cartSuccess',
      component: cartSuccess
    }, {
      path: '/user',
      name: 'user',
      meta: {
        requireAuth: true,
        keepAlive: true
      },
      component: user
    }, {
      path: '/user/userInfo',
      name: 'userEdit',
      meta: {
        requireAuth: true
      },
      component: userInfo
    }, {
      path: '/user/password',
      name: 'userPasswordEdit',
      meta: {
        requireAuth: true
      },
      component: userPasswordEdit
    }, {
      path: '/user/wallet',
      name: 'userWallet',
      meta: {
        requireAuth: true
      },
      component: userWallet
    }, {
      path: '/user/wallet/rechargeDetail',
      name: 'rechargeDetail',
      meta: {
        requireAuth: true
      },
      component: rechargeDetail
    }, {
      path: '/user/integral',
      name: 'userIntegral',
      meta: {
        requireAuth: true
      },
      component: userIntegral
    }, {
      path: '/user/order',
      name: 'userOrder',
      meta: {
        requireAuth: true
      },
      component: userOrder
    }, {
      path: '/user/orderDetail',
      name: 'userOrderDetail',
      meta: {
        requireAuth: true
      },
      component: userOrderDetail
    }, {
      path: '/user/address',
      name: 'userAddress',
      meta: {
        requireAuth: true
      },
      component: userAddress
    }, {
      path: '/user/addressDetail',
      name: 'addressDetail',
      meta: {
        requireAuth: true
      },
      component: userAddressDetail
    }, {
      path: '/user/addressAdd',
      name: 'addressAdd',
      meta: {
        requireAuth: true
      },
      component: userAddressAdd
    }, {
      path: '/user/addressEdit',
      name: 'addressEdit',
      meta: {
        requireAuth: true
      },
      component: userAddressEdit
    }, {
      path: '/user/message',
      name: 'userMessage',
      meta: {
        requireAuth: true
      },
      component: userMessage
    }, {
      path: '/user/messageDetail',
      name: 'userMessageDetail',
      meta: {
        requireAuth: true
      },
      component: userMessageDetail
    }, {
      path: '/item/searchItemList',
      name: 'searchItemList',
      component: searchItemList
    }, {
      path: '/item/itemList',
      name: 'itemList',
      component: itemList
    }, {
      path: '*',
      redirect: '/404'
    }
  ]
})
